package com.zheng.service.impl;

import com.zheng.entity.CompanyFinanceBillValidateRecord;
import com.zheng.entity.User;
import com.zheng.enum1.BillValidatorEnum;
import com.zheng.enum1.BillValidatorStatusEnum;
import com.zheng.mapper.UserMapper;
import com.zheng.service.CompanyFinanceBillValidateRecordService;
import com.zheng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private CompanyFinanceBillValidateRecordService companyFinanceBillValidateRecordService;

    @Override
    public List<User> list() {
        return userMapper.selectList();
    }

    @Override
    public String redisTestAdd() {
        Integer count = 1;
        count++;
        stringRedisTemplate.opsForValue().set("1","123"+count);

        return null;
    }

    @Override
    public String redisTestExpir() {
        System.out.printf("123");
//        stringRedisTemplate.expire("1",30, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set("2","222",30,TimeUnit.SECONDS);
//        stringRedisTemplate.opsForValue().set("2","222",30,TimeUnit.SECONDS);
        System.out.printf("成功");
        return null;
    }

    @Override
//    @Transactional
    public void test1() {
        List<CompanyFinanceBillValidateRecord> addList = new ArrayList<>();
        for (int i =0;i< BillValidatorEnum.values().length;i++){
            CompanyFinanceBillValidateRecord record = new CompanyFinanceBillValidateRecord();
            record.setBillNo(String.valueOf(i));
            record.setValidateName("name"+i);
            record.setValidateStatus(BillValidatorStatusEnum.UNCHECKED.getStatus());
            record.setCreateTime(System.currentTimeMillis());
            record.setUpdateTime(System.currentTimeMillis());

            addList.add(record);
        }

        int size = 8;
        int part1Size = size / 3;
        int part2Size = size / 3;
        int part3Size = size - part1Size - part2Size; // 确保最后一个列表包含剩余的元素

        List<CompanyFinanceBillValidateRecord> list1 = addList.subList(0, part1Size);
        List<CompanyFinanceBillValidateRecord> list2 = addList.subList(part1Size, part1Size + part2Size);
        List<CompanyFinanceBillValidateRecord> list3 = addList.subList(part1Size + part2Size, size);

        for (int i=0;i<(size/3) ;i++){

            companyFinanceBillValidateRecordService.batchSave(list1,i);
        }
    }
}
